﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
#if UNITY_EDITOR
using UnityEditor;
#endif

public class GT_NodeWorkView : GT_ViewBase
{

    Vector2 mousePos;
    int deleteNodeID = -1;

    public GT_NodeWorkView() : base("Work View") { }

    #region Main Methods
    public override void UpdateView(Rect editorRect, Rect percentageRect, Event e, GT_NodeGraph curGraph)
    {
        base.UpdateView(editorRect, percentageRect, e, curGraph);
        GUI.Box(viewRect, viewTitle, viewSkin.GetStyle("ViewBG"));

        GT_NodeUtils.DrawGrid(viewRect, 80f, 0.15f, Color.white);
        GT_NodeUtils.DrawGrid(viewRect, 40f, 0.15f, Color.white);

        GUILayout.BeginArea(viewRect);
        if (curGraph != null)
        {
            curGraph.UpdateGraphGUI(e, viewRect, viewSkin);
        }
        GUILayout.EndArea();

        ProcessEvent(e);
    }

    public override void ProcessEvent(Event e)
    {
        base.ProcessEvent(e);
        

        if (viewRect.Contains(e.mousePosition))
        {
            if (e.button == 0)
            {
                if (e.type == EventType.MouseDown)
                {


                }
                if (e.type == EventType.MouseDrag)
                {

                }
                if (e.type == EventType.MouseUp)
                {

                }
            }

            if (e.button == 1)
            {
                if(e.type == EventType.MouseDown)
                {
                    mousePos = e.mousePosition;
                    bool overNode = false;
                    deleteNodeID = -1;
                    if (curGraph != null)
                    {
                        if(curGraph.nodes.Count > 0)
                        {
                            foreach(GT_NodeBase node in curGraph.nodes)
                            {
                                if (node.nodeRect.Contains(mousePos))
                                {
                                    deleteNodeID = curGraph.nodes.IndexOf(node);
                                    overNode = true;
                                }
                            }
                        }
                    }
                    if (!overNode)
                    {
                        ProcessContextMenu(e, 0);
                    }
                    else
                    {
                        ProcessContextMenu(e, 1);
                    }
                    
                }
            }
        }
    }
    #endregion

    #region Utility Methods
    private void ProcessContextMenu(Event e, int contextID)
    {
        GenericMenu menu = new GenericMenu();
        if (contextID == 0)
        {
            menu.AddItem(new GUIContent("Create graph"), false, ContextCallback, "0");
            menu.AddItem(new GUIContent("Load graph"), false, ContextCallback, "1");

            if (curGraph != null)
            {
                menu.AddSeparator("");
                menu.AddItem(new GUIContent("Unload graph"), false, ContextCallback, "2");

                menu.AddSeparator("");
                menu.AddItem(new GUIContent("Float node"), false, ContextCallback, "3");
                menu.AddItem(new GUIContent("Add node"), false, ContextCallback, "4");
            }
        }
        else if (contextID == 1)
        {
            if (curGraph != null)
            {
                menu.AddItem(new GUIContent("Delete node"), false, ContextCallback, "5");
            }
        }

        menu.ShowAsContext();
        e.Use();
    }

    void ContextCallback(object obj)
    {
        switch (obj.ToString())
        {
            case "0":
                GT_NodePopupWindow.InitNodePopup();
                break;
            case "1":
                GT_NodeUtils.LoadGraph();
                break;
            case "2":
                GT_NodeUtils.UnloadGraph();
                break;
            case "3":
                GT_NodeUtils.CreateNode(curGraph, NodeType.Float, mousePos);
                break;
            case "4":
                GT_NodeUtils.CreateNode(curGraph, NodeType.Add, mousePos);
                break;
            case "5":
                GT_NodeUtils.DeleteNode(deleteNodeID, curGraph);
                break;
            default:
                break;
        }
    }
    #endregion
}
